package com.fat.spzx.manager.controller;

import com.fat.spzx.manager.service.SysMenuService;
import com.fat.spzx.manager.service.SysUserService;
import com.fat.spzx.manager.service.ValidateCodeService;
import com.fat.spzx.model.dto.system.LoginDto;
import com.fat.spzx.model.entity.system.SysUser;
import com.fat.spzx.model.vo.common.Result;
import com.fat.spzx.model.vo.common.ResultCodeEnum;
import com.fat.spzx.model.vo.system.LoginVo;
import com.fat.spzx.model.vo.system.SysMenuVo;
import com.fat.spzx.model.vo.system.ValidateCodeVo;
import com.fat.spzx.utils.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 登录模块接口
 * @author : fat
 */
@Tag(name = "登录模块接口")
@RestController
@RequestMapping(value = "/admin/system/index")
@Slf4j
//@CrossOrigin
public class IndexController {

    private SysUserService sysUserService;
    private ValidateCodeService validateCodeService;
    private SysMenuService sysMenuService;

    //使用构造器注入
    @Autowired
    public IndexController(SysUserService sysUserService, ValidateCodeService validateCodeService,SysMenuService sysMenuService) {
        this.sysUserService = sysUserService;
        this.validateCodeService = validateCodeService;
        this.sysMenuService = sysMenuService;
    }


    /**
     * 用户登录接口
     */
    @Operation(summary = "用户登录接口")
    @PostMapping("/login")
    public Result login(@RequestBody LoginDto loginDto){
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 用户退出登录接口
     */
    @Operation(summary = "用户退出接口")
    @GetMapping("/logout")
    public Result logout(@RequestHeader(name = "token") String token){
        //调用业务层执行操作
        log.info("用户退出登录接口执行。。。。");
        return sysUserService.logout(token);
    }

    /**
     * 生成验证码
     */
    @Operation(summary = "生成验证码")
    @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 获取用户信息
     */
    @Operation(summary = "获取用户信息")
    @GetMapping(value = "/getUserInfo")
    public Result getUserInfo(){
        return Result.build(AuthContextUtil.getUser(),ResultCodeEnum.SUCCESS);
    }

    /**
     * 获取用户菜单
     */
    @Operation(summary = "获取用户菜单")
    @GetMapping("/menus")
    public Result menus() {
        List<SysMenuVo> sysMenuVoList =  sysMenuService.findUserMenuList() ;
        return Result.build(sysMenuVoList , ResultCodeEnum.SUCCESS) ;
    }

}
